import pymysql
class MysqlUtil:
    def __init__(self,host,port,user,password,db,charset):
        self.conn = pymysql.connect(host=host,port=port,user=user,password=password,database=db,charset=charset)
        self.conn.autocommit(True)
        self.cursor = self.conn.cursor()

    def query(self,sql,params=None):
        self.cursor.execute(sql,params)
        return self.cursor.fetchall()

    def update(self,sql,params):

        try:
            self.cursor.execute(sql,params)
            self.conn.commit()
        except Exception as result:
            print(result)
            #回滚
            self.conn.rollback()

    def close(self):
        self.cursor.close()
        self.conn.close()


'''if __name__ == '__main__':
    conn = MysqlUtil(host='127.0.0.1', port=3306, user='root', password='root', db='shop', charset='utf8')
    sql = 
    conn.query(sql,params)'''